package com.wzq.upms.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wzq.permission.client.model.Pager;
import com.wzq.permission.client.model.admin.ApplicationUserModel;
import com.wzq.permission.client.model.admin.UserModel;
import com.wzq.permission.client.model.admin.UserRoleModel;
import com.wzq.permission.client.service.UserService;
import com.wzq.upms.common.utils.MD5Utils;
import com.wzq.upms.core.EasyUiData;
import com.wzq.upms.core.JsonResponse;
import com.wzq.upms.core.SessionUtils;

/**
 * 用户控制器
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/user") 
public class UserController {
	@Autowired
	private UserService userServiceClient;
	/**
	 * 用户页面
	 */
	private static final String USER_VIEW = "user/user";
	
	/**
	 * 全局用户页面
	 */
	private static final String GLOBAL_USER_VIEW = "user/globalUser";
	
	/**
	 * 用户页面
	 * @param model 模型对象
	 * @param request request对象
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("/allList") 
	public ModelAndView  allList(UserModel model,HttpServletRequest request) throws Exception{
		ModelAndView mv = new ModelAndView(GLOBAL_USER_VIEW);
		return mv;
	}
	
	/**
	 * 用户页面
	 * @param model 模型对象
	 * @param request request对象
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("/list") 
	public ModelAndView  list(UserModel model,HttpServletRequest request) throws Exception{
		ModelAndView mv = new ModelAndView(USER_VIEW);
		request.setAttribute("applicationId", model.getApplicationId());
		return mv;
	}
	
	/**
	 * 获取数据列表
	 * @param model 模型对象
	 * @param pager 分页对象
	 * @return EasyUiData
	 * @throws Exception
	 */
	@RequestMapping("/dataList") 
	@ResponseBody
	public EasyUiData<UserModel>  dataList(UserModel model,Pager pager) throws Exception{
		EasyUiData<UserModel> data = new EasyUiData<UserModel>();
		if(StringUtils.isEmpty(pager.getSort())){
		    pager.setSort("createTime");
		    pager.setOrder(Pager.DESC);
		}
		List<UserModel> userLst = userServiceClient.queryList(model, pager);
		data.setTotal(userServiceClient.queryByCount(model));
		data.setRows(userLst==null?new ArrayList():userLst);
		return data;
	}
	
	
	/**
	 * 添加或修改数据
	 * @param model 模型对象
	 * @throws Exception
	 */
	@RequestMapping("/add")
	@ResponseBody
	public JsonResponse add(UserModel model,HttpServletRequest request) throws Exception{
		JsonResponse response = new JsonResponse(JsonResponse.SUCCESS,"保存成功");
		UserModel user = SessionUtils.getUser(request);
		if(userServiceClient.isExitUserName(model.getUserName())>0){
			response.setMsg("用户已存在,请重新输入");
			response.setResult(JsonResponse.FAIL);
		}else{
		    model.setCreateUser(user.getUserName());
		    model.setUpdateUser(user.getUserName());
		    model.setUserName(model.getUserName().trim());
			model.setPassword(MD5Utils.encodePwd(model.getPassword().trim()));
			userServiceClient.add(model);
		}
		return response;
	}
	
	/**
	 * 添加或修改数据
	 * @param model 模型对象
	 * @throws Exception
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public JsonResponse edit(UserModel model,HttpServletRequest request) throws Exception{
		JsonResponse response = new JsonResponse(JsonResponse.SUCCESS,"修改成功");
		UserModel user = SessionUtils.getUser(request);
		model.setUpdateTime(new Date());
		model.setCreateUser(user.getUserName());
		model.setUpdateUser(user.getUserName());
		model.setPassword(MD5Utils.encodePwd(model.getPassword().trim()));
		userServiceClient.updateBySelective(model);
		return response;
	}
	
	/**
	 * 根据ID查找对象
	 * @param userId  
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/getById")
	@ResponseBody
	public UserModel getById(Long userId) throws Exception{
		UserModel model = userServiceClient.queryById(userId);
		return model;
	}
	
	/**
	 * 删除数据
	 * @param userIds 主键列表
	 * @param response 
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public JsonResponse delete(Long[] userIds) throws Exception{
		JsonResponse response = new JsonResponse(JsonResponse.SUCCESS,"删除成功");
		userServiceClient.logicDelete(userIds);
		return response;
	}
	
	/**
	 * 查询用户分配的系统ID
	 * @param userId 用户ID
	 * @return 
	 */
	@RequestMapping("/queryUserApplication")
	@ResponseBody
	public List<ApplicationUserModel> queryUserApplication(Long userId){
		List<ApplicationUserModel> result = userServiceClient.queryUserApplication(userId);
		return result;
	}
	
	/**
	 * 分配系统
	 * @param userId 用户ID
	 * @param applicationUserModelList 系统用户列表
	 * @return JsonResponse
	 * @throws Exception
	 */
	@RequestMapping("/allotApplication")
	@ResponseBody
	public JsonResponse allotApplication(Long userId,Long[] applicationIds,HttpServletRequest request)throws Exception{
		JsonResponse response = new JsonResponse(JsonResponse.SUCCESS,"分配系统成功");
		if(applicationIds!=null && applicationIds.length>0){
			UserModel user = SessionUtils.getUser(request);
			List<ApplicationUserModel> applicationUserModelList = new ArrayList<ApplicationUserModel>();
			for(Long id:applicationIds){
				ApplicationUserModel applicationUserModel = new ApplicationUserModel();
				applicationUserModel.setUserId(userId);
				applicationUserModel.setApplicationId(id);
				applicationUserModel.setCreateUser(user.getUserName());
				applicationUserModel.setUpdateUser(user.getUserName());
				applicationUserModelList.add(applicationUserModel);
			}
			userServiceClient.allotApplication(userId, applicationUserModelList);
		}
		return response;
	}
	
	/**
	 * 分配角色
	 * @param userId 用户ID
	 * @param applicationId 系统ID
	 * @param roleIds 角色ID集合
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/allotRole")
	@ResponseBody
	public JsonResponse allotRole(Long userId,Long applicationId,Long[] roleIds,HttpServletRequest request)throws Exception{
		JsonResponse response = new JsonResponse(JsonResponse.SUCCESS,"分配角色成功");
		if(roleIds!=null && roleIds.length>0){
			UserModel user = SessionUtils.getUser(request);
			List<UserRoleModel> userRoleModelList = new ArrayList<UserRoleModel>();
			for(Long roleId:roleIds){
				UserRoleModel userRoleModel = new UserRoleModel();
				userRoleModel.setUserId(userId);
				userRoleModel.setRoleId(roleId);
				userRoleModel.setApplicationId(applicationId);
				userRoleModel.setCreateUser(user.getUserName());
				userRoleModel.setUpdateUser(user.getUserName());
				userRoleModelList.add(userRoleModel);
			}
			userServiceClient.allotRole(userId,applicationId,userRoleModelList);
		}
		return response;
	} 
	
	/**
	 * 查询用户分配的角色ID
	 * @param userId 用户ID
	 * @param applicationId 系统ID
	 * @return List<UserRoleModel>
	 */
	@RequestMapping("/queryUserRole")
	@ResponseBody
	public List<UserRoleModel> queryUserRole(Long userId,Long applicationId){
		List<UserRoleModel> result = userServiceClient.queryUserRole(userId, applicationId);
		return result;
	}
}
